package top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.entity.AccessTokenEntity;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.entity.RefreshTokenEntity;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.repository.IOauthTokenRepository;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.dao.SysAccessTokenMapper;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.dao.SysRefreshTokenMapper;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.po.SysAccessTokenPO;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.po.SysRefreshTokenPO;

import javax.annotation.Resource;

/**
 * @author yfq
 * @date 2024/6/6 9:51
 * @description
 */
@Repository
public class OauthTokenRepository implements IOauthTokenRepository {
    @Resource
    private SysAccessTokenMapper accessTokenMapper;
    @Resource
    private SysRefreshTokenMapper refreshTokenMapper;

    @Override
    public boolean createAccessToken(AccessTokenEntity accessTokenEntity) {
        SysAccessTokenPO sysAccessTokenPO = buildSysAccessTokenPO(accessTokenEntity);
        if (sysAccessTokenPO == null) return false;

        int insert = accessTokenMapper.insert(sysAccessTokenPO);

        return insert == 1;
    }

    @Override
    public boolean createRefreshToken(RefreshTokenEntity refreshTokenEntity) {
        SysRefreshTokenPO sysRefreshTokenPO = buildSysRefreshTokenPO(refreshTokenEntity);
        if (sysRefreshTokenPO == null) return false;

        int insert = refreshTokenMapper.insert(sysRefreshTokenPO);

        return insert == 1;
    }

    @Override
    public RefreshTokenEntity queryRefreshToken(String refreshToken) {
        if (StrUtil.isEmpty(refreshToken)) return null;

        SysRefreshTokenPO sysRefreshTokenPO = refreshTokenMapper.selectOne(Wrappers.<SysRefreshTokenPO>lambdaQuery().eq(SysRefreshTokenPO::getRefreshToken, refreshToken));

        return buildRefreshTokenEntity(sysRefreshTokenPO);
    }

    @Override
    public AccessTokenEntity queryAccessToken(String refreshToken) {
        if (StrUtil.isEmpty(refreshToken)) return null;

        SysAccessTokenPO sysAccessTokenPO = accessTokenMapper.selectOne(Wrappers.<SysAccessTokenPO>lambdaQuery().eq(SysAccessTokenPO::getRefreshToken, refreshToken));

        return buildAccessTokenEntity(sysAccessTokenPO);
    }

    private AccessTokenEntity buildAccessTokenEntity(SysAccessTokenPO sysAccessTokenPO) {
        if (sysAccessTokenPO == null) return null;

        return AccessTokenEntity.builder()
                .id(sysAccessTokenPO.getId())
                .userId(sysAccessTokenPO.getUserId())
                .userType(sysAccessTokenPO.getUserType())
                .accessToken(sysAccessTokenPO.getAccessToken())
                .refreshToken(sysAccessTokenPO.getRefreshToken())
                .clientId(sysAccessTokenPO.getClientId())
                .scopes(sysAccessTokenPO.getScopes())
                .expiresTime(sysAccessTokenPO.getExpiresTime())
                .creator(sysAccessTokenPO.getCreator())
                .createTime(sysAccessTokenPO.getCreateTime())
                .updater(sysAccessTokenPO.getUpdater())
                .updateTime(sysAccessTokenPO.getUpdateTime())
                .build();
    }

    private SysAccessTokenPO buildSysAccessTokenPO(AccessTokenEntity accessTokenEntity) {
        if (accessTokenEntity == null) return null;

        SysAccessTokenPO sysAccessTokenPO = new SysAccessTokenPO();
        sysAccessTokenPO.setId(accessTokenEntity.getId());
        sysAccessTokenPO.setUserId(accessTokenEntity.getUserId());
        sysAccessTokenPO.setUserType(accessTokenEntity.getUserType());
        sysAccessTokenPO.setAccessToken(accessTokenEntity.getAccessToken());
        sysAccessTokenPO.setRefreshToken(accessTokenEntity.getRefreshToken());
        sysAccessTokenPO.setClientId(accessTokenEntity.getClientId());
        sysAccessTokenPO.setScopes(accessTokenEntity.getScopes());
        sysAccessTokenPO.setExpiresTime(accessTokenEntity.getExpiresTime());
        sysAccessTokenPO.setCreator(accessTokenEntity.getCreator());
        sysAccessTokenPO.setCreateTime(accessTokenEntity.getCreateTime());
        sysAccessTokenPO.setUpdater(accessTokenEntity.getUpdater());
        sysAccessTokenPO.setUpdateTime(accessTokenEntity.getUpdateTime());

        return sysAccessTokenPO;
    }

    private SysRefreshTokenPO buildSysRefreshTokenPO(RefreshTokenEntity refreshTokenEntity) {
        if (refreshTokenEntity == null) return null;

        SysRefreshTokenPO sysRefreshTokenPO = new SysRefreshTokenPO();
        sysRefreshTokenPO.setId(refreshTokenEntity.getId());
        sysRefreshTokenPO.setUserId(refreshTokenEntity.getUserId());
        sysRefreshTokenPO.setRefreshToken(refreshTokenEntity.getRefreshToken());
        sysRefreshTokenPO.setUserType(refreshTokenEntity.getUserType());
        sysRefreshTokenPO.setClientId(refreshTokenEntity.getClientId());
        sysRefreshTokenPO.setScopes(refreshTokenEntity.getScopes());
        sysRefreshTokenPO.setExpiresTime(refreshTokenEntity.getExpiresTime());
        sysRefreshTokenPO.setCreator(refreshTokenEntity.getCreator());
        sysRefreshTokenPO.setCreateTime(refreshTokenEntity.getCreateTime());
        sysRefreshTokenPO.setUpdater(refreshTokenEntity.getUpdater());
        sysRefreshTokenPO.setUpdateTime(refreshTokenEntity.getUpdateTime());
        sysRefreshTokenPO.setDeleted(refreshTokenEntity.getDeleted());

        return sysRefreshTokenPO;
    }

    private RefreshTokenEntity buildRefreshTokenEntity(SysRefreshTokenPO sysRefreshTokenPO) {
        if (sysRefreshTokenPO == null) return null;

        return RefreshTokenEntity.builder()
                .id(sysRefreshTokenPO.getId())
                .userId(sysRefreshTokenPO.getUserId())
                .refreshToken(sysRefreshTokenPO.getRefreshToken())
                .userType(sysRefreshTokenPO.getUserType())
                .clientId(sysRefreshTokenPO.getClientId())
                .scopes(sysRefreshTokenPO.getScopes())
                .expiresTime(sysRefreshTokenPO.getExpiresTime())
                .creator(sysRefreshTokenPO.getCreator())
                .createTime(sysRefreshTokenPO.getCreateTime())
                .updater(sysRefreshTokenPO.getUpdater())
                .updateTime(sysRefreshTokenPO.getUpdateTime())
                .deleted(sysRefreshTokenPO.getDeleted())
                .build();
    }

}
